<template>
    <label class="jul-radio" :class="{ 'is-checked' : label === model}">
        <span class="jul-radio__input">
            <span class="jul-radio__inner"></span>
            <input class="jul-radio__original"
                type="radio"
                :value="label"
                :name="name"
                v-model="model">
        </span>
        <span class="jul-radio__label">
            <slot></slot>
            <!-- 如果没有传内容，我们就把label当成内容 -->
            <template v-if="!$slots.default">
                {{label}}
            </template>
        </span>
    </label>
</template>

<script>
export default {
    name: 'JulRadio',
    inject: {
        RadioGroup: {
            default: ''
        }
    },
    // 需要提供一个计算属性
    computed: {
        // 双向绑定计算属性，不能直接写函数，需要写个对象，提供get和set函数
        model: {
            get() {
                // console.log(this.value)
                // return this.value
                return this.isGroup ? this.RadioGroup.value : this.value
            },
            set(value) {
                // 触发父组件给当前组件注册的input事件
                // this.$emit('input', value)
                this.RadioGroup ? this.RadioGroup.$emit('input', value) : this.$emit('input', value)
            }
        },
        isGroup() {
            // 这个函数用于判断radio是否被group所包裹
            // 双感叹表示先取反成为boolean类型，再取反回正常
            return !!this.RadioGroup
        }
    },
    props: {
        label: {
            type: [String, Number, Boolean],
            default: ''
        },
        value: null,
        name: {
            type: String,
            default: ''
        }
    }
}
</script>

<style lang="scss">
    .jul-radio {
        color: #606266;
        font-weight: 500;
        line-height: 1;
        position: relative;
        cursor: pointer;
        display: inline-block;
        white-space: nowrap;
        margin-right: 30px;
        outline: none;
        font-style: 14px;
        -moz-user-select: none;
        -webkit-user-select: none;
        -ms-user-select: none;
        user-select: none;
        .jul-radio__input {
            white-space: nowrap;
            cursor: pointer;
            outline: none;
            display: inline-block;
            line-height: 1;
            position: relative;
            vertical-align: middle;
            .jul-radio__inner {
                border: 1px solid #dcdfe6;
                border-radius: 100%;
                width: 14px;
                height: 14px;
                background-color: #fff;
                position: relative;
                cursor: pointer;
                display: inline-block;
                box-sizing: border-box;
                &::after {
                    width: 4px;
                    height: 4px;
                    border-radius: 100%;
                    background-color: #fff;
                    content: "";
                    position: absolute;
                    left: 50%;
                    top: 50%;
                    transform: translate(-50%, -50%) scale(0);
                    transition: transform .15s ease-in;
                }
            }
            .jul-radio__original {
                opacity: 0;
                outline: none;
                position: absolute;
                z-index: -1;
                top: 0;
                left: 0;
                right: 0;
                bottom: 0;
                margin: 0;
            }
        }
        .jul-radio__label {
            font-style: 14px;
            padding-left: 10px;
        }
    }

    // 被选中的样式
    .jul-radio.is-checked {
        .jul-radio__input {
            .jul-radio__inner {
                border-color: #5848e9;
                background: #5848e9;
                &::after {
                    transform: translate(-50%, -50%) scale(1);
                }
            }
        }
        .jul-radio__label {
            color: #5848e9;
        }
    }
</style>
